﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Security;
using System.Data;

namespace FootBallMatchProject.Administrator_WebSite
{
    public partial class loginPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                DataClassLayer.User user = new DataClassLayer.User();
                DataClassLayer.UserCollection users = new DataClassLayer.UserCollection();
                user.UserName = txtUsername.Text;
                user.Password = txtPassword.Text;
                bool rememberUserName = RememberMeSet.Checked;
                DataTable table = users.FillList(user);
                if (table.Rows.Count == 1)
                {

                    //Session["UserID"] = table.Rows[0]["UserID"].ToString();
                    //Session["UserName"] = table.Rows[0]["UserName"].ToString();
                    //Session["Role"] = table.Rows[0]["Role"].ToString();
                    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                    1, // Ticket version
                    user.UserName, // Username to be associated with this ticket
                    DateTime.Now, // Date/time ticket was issued
                    DateTime.Now.AddMinutes(50), // Date and time the cookie will expire
                    rememberUserName, // if user has chcked rememebr me then create persistent cookie
                    "", // store the user data, in this case roles of the user
                    FormsAuthentication.FormsCookiePath); // Cookie path specified in the web.config file in <Forms> tag if any.
                    // To give more security it is suggested to hash it
                    string hashCookies = FormsAuthentication.Encrypt(ticket);
                    HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashCookies); // Hashed ticket
                    // Add the cookie to the response, user browser
                    Response.Cookies.Add(cookie);
                    // Get the requested page from the url
                    string returnUrl = Request.Url.ToString();
                    // check if it exists, if not then redirect to default page
                    if (returnUrl == null || returnUrl.ToString().Contains("loginPage.aspx")) returnUrl = "~/Administrator_WebSite/Dashboard.aspx";
                    Response.Redirect(returnUrl, false);
                    return;
                }
                else
                {
                    // "اسم مستخدم او كلمة مرور خاطئة";
                    return;
                }
            }
            catch (SqlException sx)
            {
                sx = null;
                // "اسم مستخدم او كلمة مرور خاطئة";
            }
            catch (Exception ex)
            {
                ex = null;
                // "اسم مستخدم او كلمة مرور خاطئة";
            }
        }
    }
}